package com.acg.auth.controller;

import com.acg.auth.pojo.SysLoginLog;
import com.acg.auth.pojo.SysUser;
import com.acg.auth.service.SysUserService;
import com.acg.auth.vo.LoginUser;
import com.acg.exception.ACGException;
import com.acg.util.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author 没有脑袋
 * @Date 2025/7/29 11:40
 */
@RestController
@RequestMapping("/acg/index")
@CrossOrigin("*")
public class IndexController {

    @Autowired
    private SysUserService userService;

//    @Autowired
//    private SysLoginLogService loginLogService;


    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginUser user, HttpServletRequest request) throws Exception {

        // 登录日志
        SysLoginLog loginLog = new SysLoginLog();
        // 设置登录时间和创建时间
        loginLog.setAccessTime(LocalDateTime.now());
        loginLog.setAccessTime(LocalDateTime.now());
        // 设置ip地址
        loginLog.setIpaddr(IPUtils.getIpAddr(request));
        // 设置当前登录用户
        loginLog.setUsername(user.getUsername());


        // 1 从前端获取账号名和密码
        String username = user.getUsername();
        // 2 根据账号名查找数据库中是否有对应数据
        SysUser sysUser = userService.getUserByUsername(username);
        // 3.1 如果没有，则返回错误信息
//        if (ObjectUtils.isEmpty(sysUser)) {
//            saveLoginLog(loginLog, "用户名不存在",false);
//            throw new TogogoException("用户名不存在", 20001);
//        }

        // 3.2 如果有，则判断密码是否正确
        // 4.1 把用户上传的明文密码进行MD5编码 后和数据库对比
        if (!MD5.encrypt(user.getPassword()).equals(sysUser.getPassword())) {

            throw new ACGException("账号或密码错误！",500);
        }
        // 4.2 如果密码正确
        // 5.1 判断当前用户状态是否 正常
        if (sysUser.getStatus() == 0) {
//            saveLoginLog(loginLog, "用户已禁用",false);
            throw new ACGException("用户已禁用", 20004);
        }
        // 5.2用户状态正常
        // 6.1 生成token
        String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername());
        Map<String, Object> map = new HashMap<>();
        map.put("token",token);
//        saveLoginLog(loginLog, "登陆成功", true);

        return Result.ok(map);
    }

    /**
     * 注册
     */
    @OperLog(value = "注册用户")
    @PostMapping("register")
    public Result save(@RequestBody SysUser user) {
        // 把密码进行 MD5加密
        String encrypt = MD5.encrypt(user.getPassword());
        user.setPassword(encrypt);
        boolean save = userService.save(user);
        if (save) {
            return Result.ok();
        }else {
            return Result.fail("增加失败");
        }
    }

    /**
     * 获取个人信息
     */
    @GetMapping("/info")
    public Result info(HttpServletRequest request) throws Exception {
        // 从用户请求头中获取token
        String token = request.getHeader("token");

        // 如果token为空 报错
        if (StringUtils.isEmpty(token)) {
            throw new ACGException("请先登录", 20001);
        }
        String username = JwtHelper.getUsername(request.getHeader("token"));
        System.out.println("用户信息"+username);
        Map<String, Object> map = userService.getUserInfo(username);
        return Result.ok(map);
    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok();
    }


//    private void saveLoginLog(SysLoginLog loginLog, String msg,Boolean isSuccess) {
//        loginLog.setMsg(msg);
//        loginLog.setStatus(isSuccess);
//        loginLogService.save(loginLog);
//    }

}
